﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.Web.Models;
using ZhouYu.NewWorld.ViewModel;

namespace ZhouYu.NewWorld.Web.Controllers
{
    public class UsercommentController : Controller
    {

        //注入
        private IUsercommentService _usercommentService;
        public UsercommentController(IUsercommentService usercommentService)
        {
            _usercommentService = usercommentService;
        }

        #region 显示数据
        /// <summary>
        /// 显示数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public JsonResult GetUsercommentPageList(int page, int limit, string name)
        {
            PageListResult<Usercomment> resultDataSet = new PageListResult<Usercomment>();
            resultDataSet.code = 0;
            resultDataSet.msg = string.Empty;
            int count = 0;
            Expression<Func<Usercomment, bool>> Wherelambda = c => true;
            if (!string.IsNullOrEmpty(name))
            {
                Wherelambda = a => a.UserComment_Content.Contains(name);
            }
            Expression<Func<Usercomment, Nullable<int>>> OrderBylambda = c => c.User_Id;
            resultDataSet.data = _usercommentService.QueryPageList(page, limit, Wherelambda, OrderBylambda, out count);
            resultDataSet.count = count;
            return Json(resultDataSet, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public JsonResult DelUsercomment(int id)
        {
            ResponseResult ResponseResult = new ResponseResult();
            ResponseResult.Success = _usercommentService.Deletes(id);
            return Json(ResponseResult);
        }
        #endregion

        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult AddUsercomment(Usercomment usercomment)
        {
            usercomment.UserComment_CreateTime = DateTime.Now;
            usercomment.UserComment_UpdateTime = DateTime.Now;
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _usercommentService.Add(usercomment);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        /// 
        public JsonResult UpdateUsercomment(Usercomment usercomment)
        {
            ResponseResult resul = new ResponseResult();
            usercomment.UserComment_CreateTime = DateTime.Now;
            usercomment.UserComment_UpdateTime = DateTime.Now;
            resul.Success = _usercommentService.Update(usercomment);
            return Json(resul);
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 根据Id 删除多个实体
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public JsonResult BatchDeleteUsercomment(string Id)
        {
            //string转换string[]数据
            string[] strArray = Id.Split(',');
            int[] iNums;
            //string[]转换int[]
            iNums = Array.ConvertAll(strArray, int.Parse);
            ResponseResult ResponseResult = new ResponseResult();
            ResponseResult.Success = _usercommentService.BatchDeleteById(iNums);
            return Json(ResponseResult);
        }
        #endregion


        // GET: Usercomment
        public ActionResult Index()
        {
            return View();
        }
    }
}